﻿package Interface
{	
	import Core.InterfacePage;
	
	import Interface.ShopClasses.CategoryLabel;
	import Interface.ShopClasses.ShopScroller;
	import Interface.ShopClasses.ShopItem;	
	import Interface.ShopClasses.ThemeShopItem;
	import Interface.ShopClasses.VisualizationShopItem;
	
	import Interface.MessagesClasses.YesNoMessage;
	
	import flash.events.MouseEvent;	
	import flash.text.TextField;	
	import flash.display.FrameLabel;
	import flash.events.Event;	

	public class PageShop extends InterfacePage
	{	
		private const CInfoText:String = 'Для отображения информации о товаре наведите курсор на иконку нужного товара';
		private const СLabels:Array = new Array
		(
		 'Dynamics', 
		 'Equalizers', 
		 'Headphones', 
		 'Amplifiers', 
		 'Vinils', 
		 'Themes', 
		 'Visualizations'
		 );
		 
		private var DYNAMICS:int;
		private var EQUALIZERS:int;
		private var HEADPHONES:int;
		private var AMPLIFIERS:int;
		private var VINILS:int;
		private var THEMES:int;
		private var VISUALIZATIONS:int;
		
		private var FButtons:Vector.<CategoryLabel>;
		private var FScrollers:Vector.<ShopScroller>;
		private var FItems:Vector.<ShopItem>;
		private var FSelectedID:int;
		private var FInfo:TextField;
		
		public function PageShop():void
		{					
			LoadLabels();						
			
			FButtons = new Vector.<CategoryLabel>;
			FScrollers = new Vector.<ShopScroller>;
			FItems = new Vector.<ShopItem>;
			
			FInfo = getChildByName('InfoText') as TextField;
			
			AddButton(95, 'ОБОРУДОВАНИЕ').Selected = true;
			AddButton(270, 'ТЕМЫ');
			AddButton(365, 'ВИЗУАЛИЗАЦИИ');		
			
			FillShop();
			
			for(var j in FScrollers)
				FScrollers[j].SetupPages();			
			
			Reset();
		}
		
		private function LoadLabels():void
		{		
			var vTestItem:ShopItem = new ShopItem();			
			DYNAMICS = vTestItem.FrameByName(СLabels[0]);
			EQUALIZERS = vTestItem.FrameByName(СLabels[1]);
			HEADPHONES = vTestItem.FrameByName(СLabels[2]);
			AMPLIFIERS = vTestItem.FrameByName(СLabels[3]);
			VINILS = vTestItem.FrameByName(СLabels[4]);
			THEMES = vTestItem.FrameByName(СLabels[5]);
			VISUALIZATIONS = vTestItem.FrameByName(СLabels[6]);
			vTestItem = null;
		}
		
		private function FillShop():void
		{
			FScrollers[0].Add(AddDynamic(DYNAMICS, 500, 1.5));
			FScrollers[0].Add(AddDynamic(DYNAMICS + 1, 1000, 2.5));
			FScrollers[0].Add(AddDynamic(DYNAMICS + 2, 1500, 3.5));
			
			FScrollers[0].Add(AddEqualizer(EQUALIZERS, 500, 1.5));
			FScrollers[0].Add(AddEqualizer(EQUALIZERS + 1, 1000, 2.5));
			FScrollers[0].Add(AddEqualizer(EQUALIZERS + 2, 1500, 3.5));
			
			FScrollers[0].Add(AddHeadphones(HEADPHONES, 500, 1.5));
			FScrollers[0].Add(AddHeadphones(HEADPHONES + 1, 1000, 2.5));
			FScrollers[0].Add(AddHeadphones(HEADPHONES + 2, 1500, 3.5));
			
			FScrollers[0].Add(AddAmplifier(AMPLIFIERS, 500, 1.5));
			FScrollers[0].Add(AddAmplifier(AMPLIFIERS + 1, 1000, 2.5));
			FScrollers[0].Add(AddAmplifier(AMPLIFIERS + 2, 1500, 3.5));
			
			FScrollers[0].Add(AddVinil(VINILS, 500, 1.5));
			FScrollers[0].Add(AddVinil(VINILS + 1, 1000, 2.5));
			FScrollers[0].Add(AddVinil(VINILS + 2, 1500, 3.5));
			
			FScrollers[1].Add(AddTheme(THEMES, 0, 'Стандартная тема'));			
			FScrollers[1].Add(AddTheme(THEMES + 1, 0, 'Новогодняя тема'));
			
			FScrollers[2].Add(AddVisualization(VISUALIZATIONS, 0, 'Стандартная визуализация'));			
			FScrollers[2].Add(AddVisualization(VISUALIZATIONS + 1, 500, 'Вертикальный еквалайзер'));
			FScrollers[2].Add(AddVisualization(VISUALIZATIONS + 2, 500, 'Плазменный круг'));
		}
		
		private function AddTheme(AGrapic:int, APrice:int, ADescription:String):ThemeShopItem
		{
			var vItem:ThemeShopItem = new ThemeShopItem()
			AddSpecialItem(vItem, AGrapic, APrice, ADescription);
			vItem.ProfileGraphic = AGrapic - THEMES + 1;
			return vItem;
		}
		
		private function AddVisualization(AGrapic:int, APrice:int, ADescription:String):VisualizationShopItem
		{
			var vItem:VisualizationShopItem = new VisualizationShopItem()
			AddSpecialItem(vItem, AGrapic, APrice, ADescription);
			vItem.ProfileGraphic = AGrapic - VISUALIZATIONS;
			return vItem;
		}
		
		private function AddSpecialItem(AItem:ShopItem, AGrapic:int, APrice:int, ADescription:String):void
		{
			AItem.GlobalID = FItems.push(AItem) - 1;
			AItem.Graphic = AGrapic;			
			AItem.Price = APrice;			
			AItem.Description = ADescription;
			AItem.Name = ADescription;
			AddListeners(AItem);
		}
		
		private function AddButton(AX:int, AText:String):CategoryLabel
		{
			var vReulst:CategoryLabel = new CategoryLabel(AText);
			vReulst.x = AX;
			vReulst.y = 95;
			vReulst.ID = FButtons.length;
			FButtons.push(vReulst);
			vReulst.addEventListener(MouseEvent.MOUSE_DOWN, OnButtonClick);
			addChild(vReulst);
			
			var vScroller:ShopScroller = new ShopScroller();
			vScroller.x = 25;
			vScroller.y = 119;
			vScroller.visible = false;
			FScrollers.push(vScroller)
			addChild(vScroller);
			
			return vReulst;
		}
		
		private function OnButtonClick(e:MouseEvent):void
		{
			CurrentCategory = (e.target.parent as CategoryLabel).ID;
		}
		
		private function set CurrentCategory(ACatagoryID:int):void
		{
			if(ACatagoryID != FSelectedID)
			{
				FSelectedID = ACatagoryID; 
				for(var i in FButtons)
				{
					FButtons[i].Selected = false;							
					FScrollers[i].visible = false;
					FScrollers[i].Reset();
				}
				
				FButtons[ACatagoryID].Selected = true;
				FScrollers[ACatagoryID].visible = true;
			}
		}
		
		public function Reset():void
		{
			FSelectedID =-1;
			CurrentCategory = 0;
		}
		
		private function AddDynamic(AIndex:int, APrice:int, ABonus:Number):ShopItem
		{
			var vDynamic:ShopItem = AddItem(AIndex, APrice, ABonus);
			vDynamic.Type = 0;
			vDynamic.Name = 'Динамики';
			vDynamic.ProfileGraphic = AIndex - DYNAMICS + 1;
			return vDynamic;
		}
		
		private function AddEqualizer(AIndex:int, APrice:int, ABonus:Number):ShopItem
		{
			var vEqualizer:ShopItem = AddItem(AIndex, APrice, ABonus);
			vEqualizer.Type = 1;
			vEqualizer.Name = 'Еквалайзер';
			vEqualizer.ProfileGraphic = AIndex - EQUALIZERS + 1;
			return vEqualizer;
		}
		
		private function AddHeadphones(AIndex:int, APrice:int, ABonus:Number):ShopItem
		{
			var vHeadphones:ShopItem = AddItem(AIndex, APrice, ABonus);
			vHeadphones.Type = 2;
			vHeadphones.Name = 'Наушники';
			vHeadphones.ProfileGraphic = AIndex - HEADPHONES + 1;
			return vHeadphones;
		}
		
		private function AddAmplifier(AIndex:int, APrice:int, ABonus:Number):ShopItem
		{
			var vAmplifier:ShopItem = AddItem(AIndex, APrice, ABonus);
			vAmplifier.Type = 3;
			vAmplifier.Name = 'Усилитель';
			vAmplifier.ProfileGraphic = AIndex - AMPLIFIERS + 1;
			return vAmplifier;
		}
		
		private function AddVinil(AIndex:int, APrice:int, ABonus:Number):ShopItem
		{
			var vVinil:ShopItem = AddItem(AIndex, APrice, ABonus);
			vVinil.Type = 4;
			vVinil.Name = 'Винил';
			vVinil.ProfileGraphic = AIndex - VINILS + 1;			
			return vVinil;
		}
		
		private function AddItem(AIndex:int, APrice:int, ABonus:Number):ShopItem
		{
			var vItem:ShopItem = new ShopItem();
			vItem.GlobalID = FItems.push(vItem) - 1;
			vItem.Graphic = AIndex;
			vItem.Price = APrice;							
			vItem.Bonus = ABonus;
			AddListeners(vItem);
			return vItem;
		}
		
		private function AddListeners(AItem:ShopItem):void
		{
			AItem.addEventListener(MouseEvent.MOUSE_OVER, OnItemMouseOver);
			AItem.addEventListener(MouseEvent.MOUSE_OUT, OnItemMouseOut);
			AItem.addEventListener(MouseEvent.MOUSE_DOWN, OnItemClick);
		}
		
		public function GetItemById(AID:int):ShopItem
		{
			return FItems[AID];
		}
		
		private function OnItemMouseOver(e:MouseEvent):void
		{
			var vItem:ShopItem = ShopItem(e.target.parent);
			FInfo.text = vItem.Description;
		}
		
		private function OnItemMouseOut(e:MouseEvent):void
		{
			FInfo.text = CInfoText;
		}
		
		private function OnItemClick(e:MouseEvent):void
		{
			var vItem:ShopItem = ShopItem(e.target.parent);
			
			trace('GlobalID: ' + vItem.GlobalID);
			
			if(FGame.User.Money >= vItem.Price)			
				BuyItem(vItem);
			else
			{
				var vMessage:YesNoMessage = 
				FGame.ShowYesNo('На Вашем игровом счету недостаточно средств для покупки этого товара. ' +
								'Нажмите кнопку "Пополнить" для перевода голосов в приложение', 
								'Недостаточно средств');
				vMessage.YesButton.Text = 'Пополнить';
				vMessage.NoButton.Text = 'Отмена';
				vMessage.YesButton.addEventListener(MouseEvent.MOUSE_DOWN, FGame.ShowTransferMessage);
			}
		}
		
		private function BuyItem(AItem:ShopItem):void
		{
			AItem.Buy(FGame);
			FGame.ShowInfo('Вы успешно приобрели товар "' + AItem.Name + '"', 'Поздравляем с покупкой!');
			FInfo.text = AItem.Description;
		}
		
		public function set PlayerItems(AItems:Vector.<int>):void
		{
			for(var i in AItems)
			{
				if(AItems[i] > -1)
					FItems[AItems[i]].Price = 0;
			}
		}		
	}	
}
